package game;

import gfx.Background;
import gfx.GUI;
import gfx.Image;
import gfx.PressableButton;

import java.text.DecimalFormat;
import java.util.HashMap;

import javax.microedition.khronos.opengles.GL10;

import util.GameClock;
import util.VertexBuffer;
import android.graphics.PointF;

public class Stage {
	private GUI gui;
	
//	private DialogueManager dialogueManager;
	
	private HashMap<Short, Image> mobMap;
	
	private Background bg;
	
	private PressableButton[] pauseButtonArray;

	private PressableButton selectedButton;
	
	private PressableButton retryButton;
	private PressableButton quitButton;
	
	private PressableButton nextStageButton;
	private PressableButton returnToSelectionButton;
	private PressableButton victoryQuitButton;
	private PressableButton victoryRetryButton;
	
	// 00000000
	DecimalFormat nf;

	private float[] overlay = new float[12];
	
	private short world;
	private short stage;
	private short difficulty;
	// Stolen from mobManager
	// Commented out to test SegmentManager
//	private short currentSegment;
	
	private short pauseButtonIterator;
	
	private boolean isLoaded;
	private boolean hasGraphicsLoaded;
	private boolean isPaused = false;
	private boolean kill = false;
	
	private boolean backToSelection = false;

	private boolean isGameOver = false;
	private boolean isWon = false;

	private boolean reloading = false;
	private boolean loadNext = false;
	
//	private Player player;
	
	
	public Stage(short stageID, short playerType, short difficulty){
		hasGraphicsLoaded = false;
		
		load(stageID, playerType, difficulty);
	}
	
	/** Used mainly to restart the world */
	private void load(short stageID, short playerType, short difficulty){
		isLoaded = false;
		isWon = false;
		isGameOver = false;
		
		gui = new GUI();
		
//		player = new Player(playerType);
		isLoaded = false;
		
		if(bg == null)
			bg = new Background();
		
		resume();
		
		isLoaded = true;
	}
	
	public void loadGraphics(GL10 gl){
		bg.loadGraphics((short) (world * 100 + stage), gl);
		hasGraphicsLoaded = true;
	}
	
	public void update(long time){
		if(reloading){
			load((short) (world*100 + stage), (short) -1, difficulty);
			reloading = false;
			return;
		}
		if(loadNext){
			bg.reload();
			load((short) (world*100 + stage + 1), (short) -1, difficulty);
			loadNext = false;
			return;
		}
		if(isPaused)
			return;
		
		if(gui.isChanged())
			gui.update(time);
		
//		if(player.isDead()){
//			// Update Death Animation only, and blargity.
//			if(!isGameOver)
//				isGameOver = true;
//			pause();
//			return;
//		}

//		player.setTouchMove(gui.getMovement());
		
//		player.update(time);
		
		bg.update(time);
	}
	
	public void draw(GL10 gl){
		bg.draw(gl);
		
//		player.draw(gl);

		gui.draw(gl);
		
		// Move to a drawPaused()?
		if(isPaused){
			drawOverlay(gl, .75f);

			for(pauseButtonIterator = 0; pauseButtonIterator < pauseButtonArray.length; ++pauseButtonIterator)
				pauseButtonArray[pauseButtonIterator].draw(gl);
		}
	}
	
	private void drawOverlay(GL10 gl, float alpha){
		gl.glPushMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(overlay));

		gl.glColor4f(.0f, .0f, .0f, alpha);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, overlay.length / 3);

		gl.glColor4f(1, 1, 1, 1);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glPopMatrix();
	}
	
	public boolean isLoaded(){ return isLoaded; }
	public boolean hasGraphicsLoaded(){ return hasGraphicsLoaded; }
	public boolean kill(){ return kill; }
	public boolean isPaused(){ return isPaused; }
	public boolean back(){ return backToSelection; }
	public short getPlayer(){ return (short) -1; }
	public short getWorld(){ return world; }
	public short getStage(){ return stage; }
	
	public void pause(){ isPaused = true; GameClock.getInstance().pause(); }
	public void resume(){ isPaused = false; GameClock.getInstance().resume(); }
	
	private void buttonHandler(int num){
	}

	public void handleUp(PointF location) {
		if(isPaused){
			if(selectedButton != null){
				if(selectedButton.isPressed(location))
					buttonHandler(selectedButton.getNum());
				selectedButton = null;
				return;
			}
		}
		
//		if(dialogueManager.isDialoguing()){
//			dialogueManager.handleUp(location);
//			return;
//		}
		
		if(isWon){
			if(selectedButton != null){
				if(selectedButton.isPressed(location))
					buttonHandler(selectedButton.getNum());
				selectedButton = null;
				return;
			}
		}
//		player.handleUp();
		
//		player.setIsLocLocked(false);
		gui.handleUp(location);
	}

	public void handleDrag(PointF location) {
		gui.handleDrag(location);
	}

	public void handleTouch(PointF location) {
//		if(gui.getMovement().x == 0 && gui.getMovement().y == 0)
//			player.handleDown();
//		if(isPaused)
//			return;
		if(isPaused){
//			if(player.isDead()){
//				if(retryButton.isPressed(location)) selectedButton = retryButton;
//				if(quitButton.isPressed(location)) selectedButton = quitButton;
//			}
			/**else**/ for(pauseButtonIterator = 0; pauseButtonIterator < pauseButtonArray.length; ++pauseButtonIterator)
				if(pauseButtonArray[pauseButtonIterator].isPressed(location))
					selectedButton = pauseButtonArray[pauseButtonIterator];
			return;
		}
		
//		if(dialogueManager.isDialoguing()){
//			dialogueManager.handleTouch(location);
//			return;
//		}
		
		if(isWon){
			if(nextStageButton.isPressed(location)) selectedButton = nextStageButton;
			if(returnToSelectionButton.isPressed(location)) selectedButton = returnToSelectionButton;
			if(victoryQuitButton.isPressed(location)) selectedButton = victoryQuitButton;
			if(victoryRetryButton.isPressed(location)) selectedButton = victoryRetryButton;
			
			return;
		}
			
		if(gui.handleTouch(location)){
//			player.handleDown();
		}
		
	}
	
	public boolean handleBackPress(){
		if(!isPaused)
			pause();
		else resume();
		return false;
	}
}